package com.gsocket.client;

import com.gsocket.event.CheckException;
import com.gsocket.event.check.CheckDecorator;
import com.gsocket.event.client.ClientListener;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class ReceiviceThread extends Thread {
    private boolean isRun = true;
    private GSocketClient client;
    private ClientListener clientListener;

    private CheckDecorator reveCheck;

    public byte[] sendMsg;

    public void setReveCheck(CheckDecorator reveCheck) {
        this.reveCheck = reveCheck;
        if (reveCheck != null) {
            reveCheck.sendMsg = sendMsg;
        }
    }


    public ReceiviceThread(GSocketClient client, ClientListener clientListener) {
        this.client = client;
        this.clientListener = clientListener;
    }

    public ReceiviceThread(GSocketClient client, ClientListener clientListener, CheckDecorator reveCheck) {
        this.client = client;
        this.clientListener = clientListener;
        this.reveCheck = reveCheck;
    }

    @Override
    public void run() {
        while (isRun) {
            client.sendNum++;
            System.out.println(client.sendNum);
            try {
                if (client.getSocket().isClosed()) {
                    close();
                    return;
                }
                InputStream is = client.getSocket().getInputStream();
                ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
                byte[] buffer = new byte[10240];
                int ch;
                if ((ch = is.read(buffer)) == -1) {
                    close();
                    continue;
                }
                bytestream.write(buffer, 0, ch);
                byte[] data = bytestream.toByteArray();
                bytestream.close();
//                System.out.println("接收服务端的数据：" + String.format("0x%02x", data[0]));
                if (reveCheck != null && !reveCheck.check(data)) {
                    System.out.println("数据校验失败");
                    for (byte datum : data) {
                        System.out.print(String.format("%02x", datum) + " ");
                    }
                    System.out.println();
//                    throw new CheckException();
                    if (client.sendNum < 6) {
                        continue;
                    } else {
                        System.out.println("校验完全失败");
                        close();
                        throw new CheckException();
                    }
                }
//                System.out.println("接收服务端的数据：" );
//                for (byte datum : data) {
//                    System.out.print(String.format("%02x", datum) + " ");
//                }
                if (clientListener != null) {
                    if (reveCheck != null) {
                        clientListener.receive(client, reveCheck.recvMsg);
                    } else {
                        clientListener.receive(client, data);
                    }
                }
                close();
            } catch (Exception e) {
                e.printStackTrace();
                if (clientListener != null) {
                    clientListener.err(e);
                }
            }

        }
    }

    private void close() {
        try {
            isRun = false;
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
